package com.citicpub_cloud.restfull.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.citicpub_cloud.restfull.api.dto.ChapterDTO;
import com.citicpub_cloud.restfull.api.dto.MixResDTOExt;
import com.citicpub_cloud.restfull.dao.jdbc.AbstractJdbcDaoSupport;
import com.citicpub_cloud.restfull.dao.mapper.ChapterDTOMapper;

@Service
public class BookAudioDaoService extends AbstractJdbcDaoSupport {
	public MixResDTOExt getBaseInfo(Long id) {
		MixResDTOExt obj = null;
		try {
			String sql = "select title,img from tb_book_audio where id=" + id;
			
			RowMapper<MixResDTOExt> mapper = new RowMapper<MixResDTOExt>() {
	
				@Override
				public MixResDTOExt mapRow(ResultSet rs, int index) throws SQLException {
					MixResDTOExt tmpobj = new MixResDTOExt();
					tmpobj.setTitle(rs.getString("title"));
					tmpobj.setImgUrl(rs.getString("img"));
					return tmpobj;
				}
			};
			List<MixResDTOExt> list = jdbcTemplate.query(sql,mapper);
			if(list.size()>0){
				obj = list.get(0);
			}
		} catch (Exception e) {
			log.error("根据有声书id查找基本信息出错", e);
		}
		
		return obj;
	}
	
	public List<ChapterDTO> getChapterList(Long id){
		List<ChapterDTO> list = new ArrayList<ChapterDTO>();
		
		try{
			String sql = "select audio_title as title,ordernum,audio_url as chapter_url,mp3_size as chapter_size from tb_audio_info where is_delete = 1 and book_audio_id = " + id + " order by ordernum";
			list = jdbcTemplate.query(sql,new ChapterDTOMapper());
		} catch (Exception e) {
			log.error("根据有声书id查找章节列表信息出错", e);
		}
		
		return list;
	}
}
